<template>
  <el-form :model="deptFormData" class="demo-form-inline" >
    <el-form-item label="上级菜单">
      <mine-tree-select v-model="deptFormData.parentId" :options="allDept"></mine-tree-select>
    </el-form-item>
    <el-row justify="end">
      <el-col :span="12">
        <el-form-item label="部门名称">
          <el-input v-model="deptFormData.deptName" placeholder="请输入部门名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="显示排序">
          <el-input-number v-model="deptFormData.sort" class="mx-4" :min="0" :max="100" controls-position="right"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="负责人">
          <el-input v-model="deptFormData.leader" placeholder="请输入负责人名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="联系电话">
          <el-input v-model="deptFormData.mobile" placeholder="请输入联系电话"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="邮箱">
          <el-input v-model="deptFormData.email" placeholder="请输入联系邮箱"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="部门状态">
          <el-radio v-model="deptFormData.status" :label="1" size="middle">正常</el-radio>
          <el-radio v-model="deptFormData.status" :label="0" size="middle">停用</el-radio>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {ref,getCurrentInstance} from 'vue'
import MineTreeSelect from "@/components/select/mine-tree-select";
import * as deptApi from '@/api/dept/index.js'

const {ElMessage} = require("element-plus");

export default {
  name: "mine-dept-add",
  components: {MineTreeSelect},
  props: {
    allDept: {
      required: true,
      default: [],
      type: Array
    },
    deptFormData: {
      required: true,
      default: {
        parentId: "0"
      },
      type: Object
    }
  },
  setup(props) {
    const {proxy} = getCurrentInstance()
    const onSubmit = () => {
      if (props.deptFormData.id) {
        deptApi.updateById(props.deptFormData).then(res => {
          if (res.success) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            proxy.$bus.emit('submitDeptSuccess')
          } else {
            ElMessage({
              message: res.msg,
              type: 'warning',
            })
          }
        })
      } else {
        deptApi.save(props.deptFormData).then(res => {
          if (res.success) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            proxy.$bus.emit('submitDeptSuccess')
          } else {
            ElMessage({
              message: res.msg,
              type: 'warning',
            })
          }

        })
      }
    }
    return {
      onSubmit
    }
  }
}
</script>

<style scoped>

</style>
